package Tests;

import static org.junit.Assert.*;

import javax.swing.DefaultListModel;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import Client.PokerClient.src.Client;
import Client.PokerClient.src.GameController;
import Client.PokerClient.src.GameThread;
import Client.PokerClient.src.GameWindow;
import Client.PokerClient.src.ServerController;

public class ServerGTaskControllerTest {
  ServerController serverController;
  Client client;
  static Server server;
  
  @BeforeClass
  public static void BServer() {
    server = new Server(4444);
  }
  
  @AfterClass
  public static void AServer() {
    server.EndTest();
  }
  
  @Before
  public void Inicjuj() {
	client = new Client(true);
	serverController = new ServerController(client);
	client.getGameThread().ConnectToServer("127.0.0.1", "4444");
  }
  
  @Test
  public void G_ALL() throws InterruptedException {
    client.CreateGame(4, 0, 4, true, false);
    client.getGameController().setPlayersID(new String[] {"0","1","2","3"});
    client.getGameThread().getServerGTaskController().G_ALL("G_ALL_[0,0,12;0,11;0,10;0,9;0,8;][1,1,12;1,11;1,10;1,9;1,8;][2,2,12;2,11;2,10;2,9;2,8;][3,3,12;3,11;3,10;3,9;3,8;]_3");
    
    for(int i=0; i<4; i++)
      for(int j=0; j<5; j++) {
        assertEquals(12-j, client.getGameController().getPlayersDecks()[i][0][j]);
        assertEquals(i, client.getGameController().getPlayersDecks()[i][1][j]);
        assertEquals(Integer.toString(i), client.getGameController().getPlayersID()[i]);
      }
    
    client.CreateGame(2, 0, 2, true, false);
    client.getGameController().setPlayersID(new String[] {"0","1","2","3"});
    client.getGameThread().getServerGTaskController().G_ALL("G_ALL_[0,0,0;0,1;0,2;0,3;0,4;][1,1,0;1,1;1,2;1,3;1,4;]_1");
    
    for(int i=0; i<2; i++)
      for(int j=0; j<5; j++) {
        assertEquals(j, client.getGameController().getPlayersDecks()[i][0][j]);
        assertEquals(i, client.getGameController().getPlayersDecks()[i][1][j]);
        assertEquals(Integer.toString(i), client.getGameController().getPlayersID()[i]);
      }
  }
  
  @Test
  public void G_BALL() throws InterruptedException {
    client.CreateGame(4, 0, 4, true, false);
    client.getGameController().setPlayersID(new String[] {"0","1","2","3"});
    client.getGameThread().getServerGTaskController().G_BALL("G_BALL_[0,0,12;0,11;0,10;0,9;0,8;][1,1,12;1,11;1,10;1,9;1,8;][2,2,12;2,11;2,10;2,9;2,8;][3,3,12;3,11;3,10;3,9;3,8;]");
    
    for(int i=0; i<4; i++)
      for(int j=0; j<5; j++) {
        assertEquals(12-j, client.getGameController().getPlayersDecks()[i][0][j]);
        assertEquals(i, client.getGameController().getPlayersDecks()[i][1][j]);
        assertEquals(Integer.toString(i), client.getGameController().getPlayersID()[i]);
      }
    
    client.CreateGame(2, 0, 2, true, false);
    client.getGameController().setPlayersID(new String[] {"0","1","2","3"});
    client.getGameThread().getServerGTaskController().G_BALL("G_BALL_[0,0,0;0,1;0,2;0,3;0,4;][1,1,0;1,1;1,2;1,3;1,4;]");
    
    for(int i=0; i<2; i++)
      for(int j=0; j<5; j++) {
        assertEquals(j, client.getGameController().getPlayersDecks()[i][0][j]);
        assertEquals(i, client.getGameController().getPlayersDecks()[i][1][j]);
        assertEquals(Integer.toString(i), client.getGameController().getPlayersID()[i]);
      }
  }
  
  @Test
  public void G_CARDS() {
	client.CreateGame(4, 0, 4, true, true);
	
	client.getGameThread().getServerGTaskController().G_CARDS("G_CARDS_[0,12;0,11;0,10;0,9;0,8;]");
	
	for(int j=0; j<5; j++) {
      assertEquals(12-j, client.getGameController().getPlayersDecks()[0][0][j]);
      assertEquals(0, client.getGameController().getPlayersDecks()[0][1][j]);
    }
	
    client.getGameThread().getServerGTaskController().G_CARDS("G_CARDS_[1,0;1,1;1,2;1,3;1,4;]");
	
	for(int j=0; j<5; j++) {
      assertEquals(j, client.getGameController().getPlayersDecks()[0][0][j]);
      assertEquals(1, client.getGameController().getPlayersDecks()[0][1][j]);
    }
  }
  
  @Test
  public void G_PLAYERJOIN() {
	client.CreateGame(4, 0, 4, true, true);
	
	client.getGameThread().getServerGTaskController().G_PLAYERJOIN("G_PLAYERJOIN_3");  
    assertEquals(3, client.getGameController().getActualNumberOfPlayers());
    
    client.getGameThread().getServerGTaskController().G_PLAYERJOIN("G_PLAYERJOIN_2");  
    assertEquals(2, client.getGameController().getActualNumberOfPlayers());
    
    client.getGameThread().getServerGTaskController().G_PLAYERJOIN("G_PLAYERJOIN_4");  
    assertEquals(4, client.getGameController().getActualNumberOfPlayers());
  }
  
  @Test
  public void G_PLAYEROUT() {
    client.CreateGame(4, 0, 4, true, true);
		
	client.getGameThread().getServerGTaskController().G_PLAYEROUT("G_PLAYEROUT_3");
	assertEquals(3, client.getGameController().getActualNumberOfPlayers());
	
	client.getGameThread().getServerGTaskController().G_PLAYEROUT("G_PLAYEROUT_2");
	assertEquals(2, client.getGameController().getActualNumberOfPlayers());
	
	client.getGameThread().getServerGTaskController().G_PLAYEROUT("G_PLAYEROUT_4");
	assertEquals(4, client.getGameController().getActualNumberOfPlayers());
  }
  
  @After
  public void Clean() {
	client.getMainWindow().dispose();
	client.getGameThread().finalize();
	client = null;
	serverController = null;
  }
}
